package com.server.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.authority.Login;
import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.IRewardConfigService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("config/reward")
public class RewardConfigController {

	@Autowired
	private IRewardConfigService rewardConfigService;
	
	
	@RequestMapping("")
	public ModelAndView configPage(){
		ModelAndView mv = new ModelAndView("rewardConfig/configList");
		mv.addObject("projectName",ProjectInfor.PLATFORM_NAME);
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	
	@RequestMapping("list/{orgNo}")
	public void list(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String orgNo){
		DataVector<Map<String, Object>> listData = rewardConfigService.getList(new RequestInput(request),orgNo); 
		OutputUtil.returnString(response, JSONSerielizer.toJson(listData.getList(), null, null));
	}
	
	@RequestMapping("add/{orgNo}")
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response, @PathVariable String orgNo) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey);
		ModelAndView mv = new ModelAndView("common/editFrame");
		mv.addObject("pagePath","pages/rewardConfig/configForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "new");
		mv.addObject("business", "config/reward");
		mv.addObject("titleName", "新增返费规则");
		mv.addObject("orgNo", orgNo);
		mv.addObject("userName", userName);
		return mv;
	}
	
	@RequestMapping("edit/{id}")
	public ModelAndView edit(HttpServletRequest request,
			HttpServletResponse response, @PathVariable String id) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey);
		ModelAndView mv = new ModelAndView("common/editFrame");
		mv.addObject("pagePath","pages/rewardConfig/configForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "edit");
		mv.addObject("business", "config/reward");
		mv.addObject("titleName", "新增返费规则");
		mv.addObject("id", id);
		mv.addObject("userName", userName);
		return mv;
	}
	
	@RequestMapping("check/{id}")
	public ModelAndView check(HttpServletRequest request,
			HttpServletResponse response, @PathVariable String id) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey);
		ModelAndView mv = new ModelAndView("common/editFrame");
		mv.addObject("pagePath","pages/rewardConfig/configForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "check");
		mv.addObject("business", "config/reward");
		mv.addObject("titleName", "查看返费规则");
		mv.addObject("id", id);
		mv.addObject("userName", userName);
		return mv;
	}
	
	@RequestMapping("form/{id}")
	public void form(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String id){
		Map<String, Object> formData = rewardConfigService.getForm(id);
		OutputUtil.returnString(response, JSONSerielizer.toJson(formData, null, null));
	}
	@RequestMapping("save/{action}")
	public void save(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String action){
		String operator = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		String formData = request.getParameter("formData");
		Map<String, Object> inputMap = JSONSerielizer.toMap(formData);
		if( inputMap.get("LongReward")!=null && inputMap.get("LongReward").toString().equals("true") ){
			inputMap.put("LimitCount1", 999);
		}
		inputMap.remove("LongReward");
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean isSuccess = true;
		String info = null;
		if ("new".equals(action)) {
			isSuccess = rewardConfigService.add(operator, inputMap);
		}else {//不能修改，只能生成新规则
			isSuccess = rewardConfigService.add(operator, inputMap);
		}
		map.put("isSuccess", isSuccess);
		info = isSuccess ? "添加成功。" : "添加失败！";
		map.put("info", info);
		OutputUtil.returnString(response, JSONSerielizer.toJson(map, null, null));
	}
	
}
